﻿app.controller('AccidentController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.AddMeetingForm = function (model) {
        var files = document.getElementById("taskfile").files;
        var editorInstance = CKEDITOR.instances['noticecontent'];
        model.MeetingContent = editorInstance.getData();
        $scope.LoadingMsg = "正在提交,请稍后...";
        $scope.Submiting = true;
        Upload.upload({
            method: 'POST',
            url: '../Accident/AddMeetingForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "MeetingFormPageList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetMeetingPageList = function (model, page, isload) {
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../Accident/GetMeetingPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.GetDepartmentList = function (id) {
        $scope.DepartmentList = [];
        $http({
            method: 'POST',
            url: '../Organization/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.DepartmentList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.DeleteMeetingForm = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Accident/DeleteMeetingForm',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetMeetingPageList(model, page, true);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.AddInvestigateForm = function (model) {
        var files = document.getElementById("taskfile").files;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $scope.Submiting = true;
        Upload.upload({
            method: 'POST',
            url: '../Accident/AddInvestigateForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "#";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetInvestigatePageList = function (model, page, isload) {
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../Accident/GetInvestigatePageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteInvestigateForm = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Accident/DeleteInvestigateForm',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetInvestigatePageList(model, page, true);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.AddDocumentForm = function (model) {
        var files = document.getElementById("taskfile").files;
        $scope.LoadingMsg = "正在提交,请稍后...";
        $scope.Submiting = true;
        Upload.upload({
            method: 'POST',
            url: '../Accident/AddDocumentForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "DocumentFormPageList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }


    $scope.GetDocumentPageList = function (model, page, isload) {
        var condition = JSON.parse(sessionStorage.getItem(window.location.pathname));
        if (isload && condition) {
            model = condition;
            $scope.SearchModel = condition;
        } else {
            sessionStorage.setItem(window.location.pathname, JSON.stringify(model));
        }
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../Accident/GetDocumentPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }



});